﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using COFE.Core.Utils;
using System.IO;
using COFE.Core;

namespace COFE.IO.FSI
{
    public class FileSystemDirectoryController : DirectoryControllerBase<FileSystemInfo>
    {
        #region Constructor

        public FileSystemDirectoryController(FileSystemDirectoryLister directoryLister)
            : base(directoryLister)
        {
            _directoryLister = directoryLister;
            _createDirectory = (parent, name) => { return (parent as DirectoryInfo).CreateSubdirectory(name); };
            _createFile = (parent, name) => { var fi = new FileInfo(Path.Combine(parent.FullName, name)); using (var _ = fi.Create()) { }; return fi; };

            _copy = (item, taretDir) =>
            {
                if (item is FileInfo)
                {
                    copyFile(item, Path.Combine(taretDir.FullName, item.Name));
                }
                else
                    copyDirectory(item as DirectoryInfo, taretDir as DirectoryInfo);
            };
            _move = (item, taretDir) =>
            {
                string destName = Path.Combine(taretDir.FullName, item.Name);
                if (item is FileInfo) File.Move(item.FullName, destName);
                else Directory.Move(item.FullName, destName);
            };

            _rename = (item, newName) =>
                {
                    string source = item.FullName;
                    string dest = Path.Combine(Path.GetDirectoryName(item.FullName), newName);
                    if (File.Exists(source))
                        File.Move(source, dest);
                    else Directory.Move(source, dest);
                };

            _delete = (item) =>
            {
                if (item is DirectoryInfo)
                    Directory.Delete(item.FullName, true);
                else File.Delete(item.FullName);
            };

            if (directoryLister.RootPath != null && Directory.Exists(directoryLister.RootPath))
                _fileSystemWatcher = new FileSystemWatcher(directoryLister.RootPath);
            else _fileSystemWatcher = new FileSystemWatcher();

            _fileSystemWatcher.IncludeSubdirectories = true;
            _fileSystemWatcher.Changed += new FileSystemEventHandler(
                (o, e) =>
                {
                    NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath), ModifiedAction.Changed);
                }
                );

            _fileSystemWatcher.Created += new FileSystemEventHandler(
                (o, e) =>
                {
                    NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath), ModifiedAction.Created);
                }
                );

            _fileSystemWatcher.Deleted += new FileSystemEventHandler(
                (o, e) =>
                {
                    NotifyModified(convertToParsePath(e.FullPath),
                        false, ModifiedAction.Deleted);
                }
                );

            _fileSystemWatcher.Renamed += new RenamedEventHandler(
                (o, e) =>
                {
                    NotifyModified(convertToParsePath(e.FullPath),
                        Directory.Exists(e.FullPath),
                        ModifiedAction.Renamed,
                        convertToParsePath(e.OldFullPath));
                }
                );

            try
            {
                if (!(String.IsNullOrEmpty(_fileSystemWatcher.Path)))
                    _fileSystemWatcher.EnableRaisingEvents = true;
            }
            catch { }

        }

        #endregion

        #region Methods

        private string convertToParsePath(string fullFileSystemPath)
        {
            string parseName = fullFileSystemPath.Replace(_directoryLister.RootPath, "", StringComparison.InvariantCultureIgnoreCase);
            return COFEPath.FormatFullParseName(ListerKey, parseName);
        }

        private void copyDirectory(DirectoryInfo sourceDir, DirectoryInfo targetDir)
        {
            DirectoryInfo targetSourceDir = targetDir.CreateSubdirectory(sourceDir.Name);
            foreach (var entry in sourceDir.GetFileSystemInfos())
            {
                string destName = Path.Combine(targetSourceDir.FullName, entry.Name);
                if (entry is FileInfo)
                {
                    copyFile(entry, destName);
                }
                else copyDirectory(entry as DirectoryInfo, targetSourceDir);
            }
        }

        private static void copyFile(FileSystemInfo entry, string destName)
        {
            if (File.Exists(destName))
                File.Delete(destName);
            File.Copy(entry.FullName, destName);
        }


        #endregion

        #region Data

        FileSystemWatcher _fileSystemWatcher;
        new FileSystemDirectoryLister _directoryLister;

        #endregion

        #region Public Properties

        public FileSystemWatcher FileSystemWatcher { get { return _fileSystemWatcher; } }

        #endregion
    }
}
